home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Free Software Collection: Marty 1
/
FM Towns Marty 1 Free Software Collection.iso
/
tool
/
book
/
src
/
bin.c
< prev
next >
Wrap
Text File
|
1993-11-11
|
4KB
|
186 lines
/*
* BinTree
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <egb.h>
#include <mos.h>
#include "oaklib.h"
#include "book.h"
#include "lib.h"
#define HASH_MAX 16
#define HASH(c) ((c)&15)
typedef struct _DP {
struct _DP *left;
struct _DP *right;
int len;
char *str;
char *snd;
} DATA;
static DATA *top[HASH_MAX] ;
extern char gwork[] ;
static DATA *search( char *str )
{
int cd ;
REGS DATA *dp ;
dp = top[HASH(*str)] ;
while( dp != NULL )
{
if( ( cd = strncmp( str, dp->str, dp->len ) ) == 0 )
return dp ;
else if ( cd > 0 )
dp = dp->left ;
else
dp = dp->right ;
}
return NULL ;
}
/* 指定の文字列の中に登録文字列があったら、読み上げる */
#define X(x) ((x)*8+XMIN)
#define CC 2
int snd_str( int y, char *buf, int bytes )
{
int status = ERR + 1 ;
int pos = 0 ;
int n, xpos ;
int flg_line = FALSE ;
DATA *dp ;
char *str = buf ;
DSP_writePage( gwork, 0 ) ;
EGB_writeMode( gwork, 4 ) ;
while( pos < bytes )
{
if( ( dp = search( str ) ) != NULL )
{
if( flg_line == FALSE ) /* 見つかったら、アンダーラインをひく */
{
dsp_box_clip( XMIN,y, XMAX,y, CC,CC,CC ) ;
dsp_box_clip( XMIN,y+15, XMAX,y+15, CC,CC,CC ) ;
flg_line = TRUE ;
}
xpos = calc_pos( buf, pos ) ;
dsp_box_clip( X(xpos),y+1, X(xpos+dp->len)-1,y+14, CC,CC,CC ) ;
status = snd_play( dp->snd ) ;
dsp_box_clip( X(xpos),y+1, X(xpos+dp->len)-1,y+14, CC,CC,CC ) ;
if( status == ERR )
break ;
n = dp->len ;
}
else if( iskanji(*str) && iskanji2(*(str+1)) )
n = 2 ;
else
n = 1 ;
str += n ;
pos += n ;
}
if( flg_line == TRUE ) /* アンダーラインがあるなら、消す */
{
dsp_box_clip( XMIN,y, XMAX,y, CC,CC,CC ) ;
dsp_box_clip( XMIN,y+15, XMAX,y+15, CC,CC,CC ) ;
}
EGB_writeMode( gwork, 0 ) ;
DSP_writePage( gwork, 1 ) ;
return( status ) ;
}
static DATA *new_tree( char *str, char *snd )
{
REGS DATA *dp ;
auto int cd ;
auto DATA *tp ;
auto DATA tmp ;
cd = 1 ;
tmp.left = top[ HASH(*str) ] ;
dp = &tmp ;
for( ; ; )
{
if( cd > 0 ) {
if( dp->left == NULL )
break;
dp = dp->left;
} else {
if( dp->right == NULL )
break;
dp = dp->right;
}
if( (cd = strcmp(str,dp->str)) == 0 )
return dp;
}
if( ( tp = (DATA *)malloc( sizeof( DATA ) ) ) == NULL )
return NULL ;
tp->left = tp->right = NULL;
tp->len = strlen(str);
tp->str = strdup(str);
tp->snd = strdup(snd);
if( cd > 0 )
dp->left = tp;
else
dp->right = tp;
top[ HASH(*str) ] = tmp.left ;
return tp ;
}
/*
* onsei.dicを読み込んで、辞書を作っておく
*
* 辞書がない、辞書の内容が空である、などの場合はERRを返す
*/
int init_tree( char *file )
{
auto FILE *fp ;
auto char *str, *snd ;
auto char buf[BUFSIZ] ;
REGS char *p ;
auto int num = 0 ;
if( ( fp = fopen( file, "r" ) ) == NULL )
return ERR ;
while( fgets( buf, BUFSIZ, fp ), !feof( fp ) )
{
if( buf[0] == '#' || buf[0] == '\n' )
continue ;
if( ( p = strchr( buf, '\n' ) ) != NULL )
*p = '\0' ;
p = buf ;
while( isspace(*p) ) p++;
str = p;
while( !isspace(*p) && *p != '\0' ) p++;
if( *p != '\0' )
*(p++) = '\0';
while( isspace(*p) ) p++;
snd = p;
if( *str != '\0' && *snd != '\0' &&
new_tree( str, snd ) == NULL )
{
fclose( fp ) ;
return ERR ;
}
num ++ ; /* 登録したものの数 */
}
fclose( fp ) ;
return ( num > 0 ) ? ERR+1 : ERR ;
}